//
//  TRoadPullRefreshTableViewController.m
//  TRoad2
//
//  Created by ZhangYanhua on 15/9/14.
//  Copyright (c) 2015年 Mach. All rights reserved.
//

#import "TRoadPullRefreshTableViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface TRoadPullRefreshTableViewController ()

@property (nonatomic, assign) CGFloat originBottomInset;

@end


@implementation TRoadPullRefreshTableViewController

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        if ([self conformsToProtocol:@protocol(TRoadPullRefreshProtocol) ]) {
            self.refreshDelegate = (id<TRoadPullRefreshProtocol>)self;
        } else {
            NSAssert(NO, @"子类必须要实现TRoadPullRefreshProtocol");
            self.refreshDelegate = (id<TRoadPullRefreshProtocol>)self;
        }
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([self conformsToProtocol:@protocol(TRoadPullRefreshProtocol)]) {
            self.refreshDelegate = (id<TRoadPullRefreshProtocol>)self;
        } else {
            NSAssert(NO, @"子类必须要实现TRoadPullRefreshProtocol");
            self.refreshDelegate = (id<TRoadPullRefreshProtocol>)self;
        }
    }
    return self;
}

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;

    self.TRDRefreshControl = nil;
    [self.refreshControl endRefreshing];
    [self.refreshControl removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.loadingHandler = [TRoadLoadingHandler loadingHandlerWithTableView:self.tableView
                                                       pullRefreshDelegate:self.refreshDelegate];

    self.enableMore = YES;
    self.enableRefresh = YES;

    self.TRDRefreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl = self.TRDRefreshControl;
    [self.refreshControl addTarget:self
                            action:@selector(startLoading)
                  forControlEvents:UIControlEventValueChanged];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.originBottomInset = self.bottomLayoutGuide.length + self.extraBottomInset;
}

- (void)setExtraBottomInset:(CGFloat)extraBottomInset
{
    _extraBottomInset = extraBottomInset;
    
    if ([self isViewLoaded]) {
        self.originBottomInset = self.bottomLayoutGuide.length + self.extraBottomInset;
    }
}

#pragma mark -
#pragma mark Forward methods

- (void)stopLoading;
{
    [self.loadingHandler stopLoading];
}

- (void)stopMoreLoading;
{
    [self.loadingHandler stopMoreLoading];
}

- (void)startLoadingProgrammatically;
{
    [self.loadingHandler startLoadingProgrammatically];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.loadingHandler scrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.loadingHandler scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

#pragma mark -
#pragma mark setters and getters and private methods

- (void)setRefreshDelegate:(id<TRoadPullRefreshProtocol>)refreshDelegate
{
    _refreshDelegate = refreshDelegate;
    [self.loadingHandler setRefreshDelegate:refreshDelegate];
}

- (void)setTRDRefreshControl:(UIRefreshControl *)TRDRefreshControl
{
    _TRDRefreshControl = TRDRefreshControl;
    [self.loadingHandler setTRDRefreshControl:TRDRefreshControl];
}

- (void)setOriginBottomInset:(CGFloat)originBottomInset
{
    _originBottomInset = originBottomInset;
    [self.loadingHandler setOriginBottomInset:originBottomInset];
}

- (void)setEnableRefresh:(BOOL)enableRefresh
{
    _enableRefresh = enableRefresh;
    if (enableRefresh) {
        [self.TRDRefreshControl removeFromSuperview];
    } else {
        [self.tableView addSubview:self.TRDRefreshControl];
    }
}

- (void)setEnableMore:(BOOL)enableMore
{
    _enableMore = enableMore;
    [self.loadingHandler setEnableMore:enableMore];
}

- (BOOL)isLoading
{
    return self.loadingHandler.isLoading;
}

- (BOOL)isMoreLoading
{
    return self.loadingHandler.isMoreLoading;
}

@end
